/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vistas;

import entidades.ProveedorRepuesto;
import entidades.Tanque;
import entidades.TipoCombustible;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import reportes.GenReportes;
import rn.CombustibleRN;
import rn.RepuestoRN;

/**
 *
 * @author Pablo
 */
public class IngresoCombustible extends javax.swing.JFrame {

    public IngresoCombustible() {
        initComponents();
        llenarCombos();
        mostrarDisponible((Tanque) combo_tanques.getSelectedItem());
        tf_hora.setText(getHora());
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        tf_combustible_ingresado.addKeyListener(new CambiarCampo());
        combo_origen_del_combustible.addKeyListener(new CambiarCampo());
        combo_proveedor.addKeyListener(new CambiarCampo());
        calendario.addKeyListener(new CambiarCampo());
        tf_hora.addKeyListener(new CambiarCampo());
        tf_nro_factura.addKeyListener(new CambiarCampo());
        
        bt_ingresar.addKeyListener(new IngresarCombustible());
        
        tf_combustible_ingresado.requestFocus();
    }

    public void llenarCombos() {
        List<Tanque> toTanques = CombustibleRN.getTodosVechiculos();
        List<ProveedorRepuesto> toproveedores = RepuestoRN.getTodosProveedorRepuesto();

        llenarCombo(combo_tanques, toTanques);
        llenarCombo(combo_proveedor, toproveedores);
    }

    public class CambiarCampo extends KeyAdapter {

        public void keyPressed(KeyEvent ke) {
            if (ke.getKeyCode() == KeyEvent.VK_ENTER && ke.getSource() == tf_combustible_ingresado) {
                combo_origen_del_combustible.requestFocus();
                tf_combustible_ingresado.setBackground(Color.WHITE);
            }
            if (ke.getKeyCode() == KeyEvent.VK_ENTER && ke.getSource() == combo_origen_del_combustible) {
                String s = (String) combo_origen_del_combustible.getSelectedItem();
                if (s.equals("Compra")) {
                    tf_nro_factura.setBackground(new Color(204, 255, 255));
                    tf_nro_factura.requestFocus();
                } else {
                    combo_proveedor.requestFocus();
                }
            }
            if (ke.getKeyCode() == KeyEvent.VK_ENTER && ke.getSource() == tf_nro_factura) {
                tf_nro_factura.setBackground(Color.WHITE);
                combo_proveedor.requestFocus();
            }

            if (ke.getKeyCode() == KeyEvent.VK_ENTER && ke.getSource() == combo_proveedor) {
                tf_nro_factura.setBackground(Color.WHITE);
                calendario.requestFocus();
            }
            if (ke.getKeyCode() == KeyEvent.VK_ENTER && ke.getSource() == calendario) {
                tf_hora.requestFocus();
                tf_hora.setBackground(new Color(204, 255, 255));
            }
            if (ke.getKeyCode() == KeyEvent.VK_ENTER && ke.getSource() == tf_hora) {
                tf_hora.setBackground(Color.WHITE);
                bt_ingresar.requestFocus();
            }
        }
    }

    public void llenarCombo(JComboBox combo, List lista) {
        combo.removeAllItems();
        for (Object c : lista) {
            combo.addItem(c);
        }
    }

    private void mostrarDisponible(Tanque tanque) {
        if (combo_tanques.getSelectedItem() != null) {
            tf_disponible.setText("" + tanque.getDisponible_litros());
        }
    }

    private String getHora() {
        Calendar calendario = Calendar.getInstance();
        int hora, minutos, segundos;
        hora = calendario.get(Calendar.HOUR_OF_DAY);
        minutos = calendario.get(Calendar.MINUTE);
        //segundos = calendario.get(Calendar.SECOND);
        return hora + ":" + minutos;
    }

    private class IngresarCombustible extends KeyAdapter {

        public void keyPressed(KeyEvent ke) {
            ingresarCombustible();
        }
    }

    private void ingresarCombustible() {
        blanquearCampos();
        if (chaquearCamposRequeridos()) {
            if (verificarValor(tf_combustible_ingresado.getText())) {
                if (verificarCapacidadTanque()) {
                    entidades.IngresoCombustible ing = new entidades.IngresoCombustible();
                    Tanque t = (Tanque) combo_tanques.getSelectedItem();
                    ing.setTanque(t);
                    ing.setFecha(calendario.getDate());
                    ing.setHora(tf_hora.getText());
                    ing.setDisponibilidad(Double.parseDouble(tf_disponible.getText()));
                    ing.setLitros_ingresados(Double.parseDouble(tf_combustible_ingresado.getText()));
                    ing.setNro_factura(tf_nro_factura.getText());
                    ing.setObservaciones(text_area_observaciones.getText());
                    ing.setOrigenCombustible((String) combo_origen_del_combustible.getSelectedItem());
                    ing.setTipoCombustible(t.getTipoCombustible());
                    ing.setResponsable(Principal.usuario);
                    ing.setProveedor((ProveedorRepuesto) combo_proveedor.getSelectedItem());

                    CombustibleRN.create(ing);

                    //Ahora debemos aumentar la disponibilidad del tanque y agregar el ingreso.
                    List<entidades.IngresoCombustible> ls_ingresos = t.getLs_ingresos();
                    ls_ingresos.add(ing);

                    t.setLs_ingresos(ls_ingresos);
                    t.setDisponible_litros(getSumaDecimales(t.getDisponible_litros(), tf_combustible_ingresado.getText()));
                    CombustibleRN.edit(t);

                    JOptionPane.showMessageDialog(null, "Carga realizada!!!");
                    limpiarCampos();

                    imprimirReporte();
                } else {
                    tf_combustible_ingresado.setBackground(Color.PINK);
                    JOptionPane.showMessageDialog(null, "El valor ingresado excede las capacidades del tanque");
                }

            } else {
                JOptionPane.showMessageDialog(null, "El valor ingresado no es valido");
                tf_combustible_ingresado.setBackground(Color.PINK);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Campos requeridos no pueden estar vacios!!!");
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        tf_combustible_actual1 = new javax.swing.JTextField();
        bt_ingresar1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        tf_combustible_actual4 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        tf_hora = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        tf_disponible = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        text_area_observaciones = new javax.swing.JTextArea();
        combo_tanques = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        combo_proveedor = new javax.swing.JComboBox();
        calendario = new org.freixas.jcalendar.JCalendarCombo();
        tf_combustible_ingresado = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        tf_nro_factura = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        tf_combustible = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        tf_capacidad = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        combo_origen_del_combustible = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        bt_cancelar = new javax.swing.JButton();
        bt_ingresar = new javax.swing.JButton();
        bt_limpiar = new javax.swing.JButton();

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setText("Combustible Actual (M3):");

        tf_combustible_actual1.setEnabled(false);

        bt_ingresar1.setText("INGRESAR");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setText("Ingreso (M3):");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Ingreso (M3):");

        tf_combustible_actual4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 0, 153));

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("INGRESO DE COMBUSTIBLE");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        tf_hora.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Fecha:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Tanque:");

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel14.setText("Observaciones:");

        tf_disponible.setEditable(false);
        tf_disponible.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        tf_disponible.setForeground(new java.awt.Color(51, 0, 204));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Ingreso (Ltrs):");

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel13.setText("Proveedor:");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("Tipo de Combustible:");

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("Nº Factura:");

        text_area_observaciones.setColumns(20);
        text_area_observaciones.setFont(new java.awt.Font("Monospaced", 1, 13)); // NOI18N
        text_area_observaciones.setRows(5);
        jScrollPane1.setViewportView(text_area_observaciones);

        combo_tanques.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_tanques.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_tanquesActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Hora:");

        combo_proveedor.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_proveedor.setEnabled(false);

        tf_combustible_ingresado.setBackground(new java.awt.Color(204, 255, 255));
        tf_combustible_ingresado.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Disponible (Ltrs):");

        tf_nro_factura.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        tf_nro_factura.setEnabled(false);

        jLabel11.setText("(Ejemplo: 1500.5)");

        tf_combustible.setEditable(false);
        tf_combustible.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel15.setText("Capacidad:");

        tf_capacidad.setEditable(false);
        tf_capacidad.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel16.setText("Origen:");

        combo_origen_del_combustible.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_origen_del_combustible.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Otro", "Compra", "Otra Cisterna" }));
        combo_origen_del_combustible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_origen_del_combustibleActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                    .addGap(173, 173, 173)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(tf_disponible, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(combo_tanques, 0, 240, Short.MAX_VALUE)
                                .addComponent(tf_combustible, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(tf_capacidad)))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                            .addGap(125, 125, 125)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(calendario, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel3Layout.createSequentialGroup()
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(tf_hora, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                            .addGap(105, 105, 105)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tf_combustible_ingresado, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel13)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(combo_proveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel12)
                                    .addComponent(jLabel16, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tf_nro_factura, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(combo_origen_del_combustible, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel11)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(combo_tanques, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(tf_capacidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(tf_combustible, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_disponible, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_combustible_ingresado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(combo_origen_del_combustible, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(tf_nro_factura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(combo_proveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(calendario, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_hora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        bt_cancelar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bt_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_cancelar.setText("CANCELAR");
        bt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelarActionPerformed(evt);
            }
        });

        bt_ingresar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bt_ingresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept.png"))); // NOI18N
        bt_ingresar.setText("INGRESAR");
        bt_ingresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_ingresarActionPerformed(evt);
            }
        });

        bt_limpiar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bt_limpiar.setText("LIMPIAR");
        bt_limpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_limpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bt_cancelar)
                .addGap(18, 18, 18)
                .addComponent(bt_ingresar, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(bt_limpiar)
                .addGap(176, 176, 176))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bt_ingresar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bt_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(bt_limpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 128, Short.MAX_VALUE))
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void combo_tanquesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_tanquesActionPerformed
        if (combo_tanques.getSelectedItem() != null) {
            Tanque t = (Tanque) combo_tanques.getSelectedItem();
            tf_combustible.setText(t.getTipoCombustible().toString());
            tf_capacidad.setText(t.getCapacidad_litros());
            tf_disponible.setText("" + t.getDisponible_litros());
            mostrarDisponible(t);
            tf_combustible_ingresado.requestFocus();
        }
    }//GEN-LAST:event_combo_tanquesActionPerformed

    private void bt_ingresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_ingresarActionPerformed
        ingresarCombustible();
    }//GEN-LAST:event_bt_ingresarActionPerformed

    private void combo_origen_del_combustibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_origen_del_combustibleActionPerformed
        String s = (String) combo_origen_del_combustible.getSelectedItem();
        ProveedorRepuesto p = RepuestoRN.getProveedorNO_ESP();
        List<ProveedorRepuesto> ls = RepuestoRN.getTodosProveedorRepuesto();
        llenarCombo(combo_proveedor, ls);
        if (!s.equals("Compra")) {
            tf_nro_factura.setEnabled(false);
            combo_proveedor.setEnabled(false);
            combo_proveedor.setSelectedItem(p);
        } else {
            tf_nro_factura.setEnabled(true);
            combo_proveedor.setEnabled(true);
        }
    }//GEN-LAST:event_combo_origen_del_combustibleActionPerformed

    private void bt_limpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_limpiarActionPerformed
        limpiarCampos();
    }//GEN-LAST:event_bt_limpiarActionPerformed

    private void bt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_bt_cancelarActionPerformed

    private boolean chaquearCamposRequeridos() {

        ArrayList<JTextField> campos_requeridos = new ArrayList();
        campos_requeridos.add(tf_combustible_ingresado);

        String s = (String) combo_origen_del_combustible.getSelectedItem();
        if (s.equals("Compra")) {
            campos_requeridos.add(tf_nro_factura);
        }

        for (JTextField t : campos_requeridos) {
            if (t.getText().startsWith(" ") || t.getText().isEmpty()) {
                t.setBackground(Color.PINK);
                return false;
            }
        }

        return true;
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IngresoCombustible.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IngresoCombustible.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IngresoCombustible.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IngresoCombustible.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IngresoCombustible().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cancelar;
    private javax.swing.JButton bt_ingresar;
    private javax.swing.JButton bt_ingresar1;
    private javax.swing.JButton bt_limpiar;
    private org.freixas.jcalendar.JCalendarCombo calendario;
    private javax.swing.JComboBox combo_origen_del_combustible;
    private javax.swing.JComboBox combo_proveedor;
    private javax.swing.JComboBox combo_tanques;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea text_area_observaciones;
    private javax.swing.JTextField tf_capacidad;
    private javax.swing.JTextField tf_combustible;
    private javax.swing.JTextField tf_combustible_actual1;
    private javax.swing.JTextField tf_combustible_actual4;
    private javax.swing.JTextField tf_combustible_ingresado;
    private javax.swing.JTextField tf_disponible;
    private javax.swing.JTextField tf_hora;
    private javax.swing.JTextField tf_nro_factura;
    // End of variables declaration//GEN-END:variables

    private boolean verificarValor(String text) {
        return (text.matches("[+]?\\d*(\\.\\d+)?") && text.equals("") == false);
    }

    private void limpiarCampos() {
        tf_nro_factura.setText("");
        tf_disponible.setText("");
        tf_combustible_ingresado.setText("");
        text_area_observaciones.setText("");
        blanquearCampos();
        llenarCombos();
        tf_hora.setText(getHora());

    }

    private boolean verificarCapacidadTanque() {
        Tanque t = (Tanque) combo_tanques.getSelectedItem();
        String ingresado = tf_combustible_ingresado.getText();
        BigDecimal ingr = new BigDecimal(ingresado);
        BigDecimal ltro_actual = new BigDecimal(t.getDisponible_litros());
        BigDecimal capacidad = new BigDecimal(t.getCapacidad_litros());
        BigDecimal total = ingr.add(ltro_actual);

        if (capacidad.compareTo(total) >= 0) {
            return true;
        }
        return false;
    }

    private double getSumaDecimales(double n1, String n2) {
        BigDecimal suma = new BigDecimal(n1).add(new BigDecimal(n2));
        return suma.floatValue();
    }

    private void blanquearCampos() {
        tf_combustible_ingresado.setBackground(Color.WHITE);
        tf_nro_factura.setBackground(Color.WHITE);
    }

    private int getIndiceProveedorNoESP() {
        List<ProveedorRepuesto> toproveedores = RepuestoRN.getTodosProveedorRepuesto();
        int i = 0;
        for (ProveedorRepuesto p : toproveedores) {
            if (p.getDescripcion().equals("NO ESP")) {
                return i;
            }
            i++;
        }
        return -1;
    }

    private void imprimirReporte() {
        Long id = CombustibleRN.getIdUltimoIngreso();
        GenReportes g = new GenReportes();
        g.repIngresoCombustible(id);
    }

}
